package org.csu.secondhand.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.secondhand.api.entity.Buying;
import org.csu.secondhand.api.entity.Dealing;
import org.csu.secondhand.api.persistence.BuyingMapper;
import org.csu.secondhand.api.persistence.DealingMapper;
import org.csu.secondhand.api.service.BuyingService;
import org.csu.secondhand.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("buyingService")
public class BuyingServiceImpl implements BuyingService {
    @Autowired
    private BuyingMapper buyingMapper;
    @Autowired
    private DealingMapper dealingMapper;

    @Override
    public Buying insertBuying(Buying buying) {
        buyingMapper.insert(buying);
        return buying;
    }

    @Override
    public List<Dealing> getSellingByUser(String id) {
        List<Buying> sellingList = getSellingList(id);
        List<Dealing> dealingList = new ArrayList<>();
        for (int i = 0; i < sellingList.size(); i++) {
            Dealing dealing = dealingMapper.selectById(sellingList.get(i).getDealing());
            dealingList.add(dealing);
        }
        return dealingList;
    }

    @Override
    public List<Dealing> getBuyingByUser(String id) {
        QueryWrapper<Buying> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", id);
        List<Buying> buyingList = buyingMapper.selectList(queryWrapper);
        List<Dealing> dealingList = new ArrayList<>();
        for (int i = 0; i < buyingList.size(); i++) {
            Dealing dealing = dealingMapper.selectById(buyingList.get(i).getDealing());
            dealingList.add(dealing);
        }
        return dealingList;
    }

    @Override
    public List<Buying> getBuyingList(String id) {
        QueryWrapper<Buying> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", id);
        List<Buying> buyingList = buyingMapper.selectList(queryWrapper);
        return buyingList;
    }

    @Override
    public List<Buying> getSellingList(String id) {
        List<Buying> sellingList = new ArrayList<>();
        List<Dealing> dealingList = dealingMapper.selectList(new QueryWrapper<Dealing>().eq("username", id));
        for(int i=0;i<dealingList.size();i++) {
            List<Buying> buyingList = buyingMapper.selectList(new QueryWrapper<Buying>().eq("dealing", dealingList.get(i).getId()));
            sellingList.addAll(buyingList);
        }
        return sellingList;
    }

    @Override
    public Buying removeBuying(long id) {
        Buying buying = buyingMapper.selectById(id);
        buyingMapper.deleteById(id);
        return buying;
    }

    @Override
    public Buying changeStatus(long id, String staus) {
        Buying buying = buyingMapper.selectById(id);
        buying.setStatus(staus);
        if(staus.equals("已完成")) {
            buying.setFtime(TimeUtil.getTime());
        }
        buyingMapper.updateById(buying);
        if(staus.equals("已接受")) {
            Dealing dealing = dealingMapper.selectById(buying.getDealing());
            dealing.setStatus(dealing.getStatus()-1);
            dealingMapper.updateById(dealing);
        }
        if(staus.equals("已拒绝")) {
            Dealing dealing = dealingMapper.selectById(buying.getDealing());
            dealing.setStatus(dealing.getStatus());
            dealingMapper.updateById(dealing);
        }
        return buying;
    }

    @Override
    public Buying getBuyingById(long id) {
        return buyingMapper.selectById(id);
    }

    @Override
    public Dealing getDealingByBuying(long id) {
        Buying buying = buyingMapper.selectById(id);
        return dealingMapper.selectOne(new QueryWrapper<Dealing>().eq("id", buying.getDealing()));
    }

}
